package com.sww.test.controller;


import com.alibaba.fastjson.JSON;
import com.sww.test.entity.Student;
import com.sww.test.entity.User;
import com.sww.test.mode.persons;
import com.sww.test.service.IPersonService;
import com.sww.test.service.PersonService;
import com.sww.test.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * springMVC 的例子
 */

@Controller
public class MainController {
    @Autowired
   private PersonService service;
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(){
        return "index";
    }
    @RequestMapping("/getAllStudent")
    public void getAllStudent(HttpServletRequest request, HttpServletResponse response){
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 5; i++){
            Student student = new Student();
            student.setAge(10 + i);
            student.setName("shiww" + i);
            list.add(student);
        }

        ResponseUtils.renderJson(response, JSON.toJSONString(list));

    }

    @RequestMapping("/getAllPersons")
    public void getAllPersons(HttpServletRequest request, HttpServletResponse response){

        List<persons> list = service.querypersons(new persons());
        ResponseUtils.renderJson(response, JSON.toJSONString(list));
    }


//    @RequestMapping(value = "/unlock", method = RequestMethod.POST,consumes = "application/json")
//    @ResponseBody
//    public Object unlock(@RequestBody User user){
//
//
//
//    }
}
